package com.vaadin.tutorial.crm.ui.view.list;

import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.grid.GridVariant;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.data.value.ValueChangeMode;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.tutorial.crm.backend.entity.Teacher;
import com.vaadin.tutorial.crm.backend.service.TeacherService;
import com.vaadin.tutorial.crm.ui.MainLayout;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.RequestMapping;


@Route(value = "teacher-list", layout = MainLayout.class)
@PageTitle("teacherList")
@Tag("teacherList-view")
public class TeacherView extends VerticalLayout {
    private TeacherService teacherService;
    private TeacherForm teacherForm;

    private Grid<Teacher> grid = new Grid<>(Teacher.class);
    private TextField filterText = new TextField();

    public TeacherView(TeacherService teacherService) {
        this.teacherService = teacherService;
        configureGrid();

        //表单
        teacherForm = new TeacherForm();
        teacherForm.addListener(TeacherForm.SaveEvent.class, this::saveTeacher);
        teacherForm.addListener(TeacherForm.DeleteEvent.class, this::deleteTeacher);
        teacherForm.addListener(TeacherForm.CloseEvent.class, e -> closeEditor());

        //将表格、表单添加到一个div
        Div div = new Div(grid, teacherForm);
        div.addClassName("main");
        div.setSizeFull();

        //将toolbar、div添加到界面
        add(getToolbar(), div);
        updateList("");
        closeEditor();
    }
    
    private void configureGrid() {
        grid.addClassName("teacher-grid");
        grid.addThemeVariants(GridVariant.LUMO_ROW_STRIPES);
        grid.removeColumnByKey("teacherId");
        grid.removeColumnByKey("teacherPhone");
        grid.removeColumnByKey("teacherNum");
        grid.removeColumnByKey("teacherName");
//        grid.setSizeFull();
        grid.addColumn("teacherNum").setHeader("编号");
        grid.addColumn("teacherName").setHeader("姓名");
        grid.addColumn("teacherPhone").setHeader("联系电话");
        grid.getColumns().forEach(col -> col.setAutoWidth(true));

        //点击表格中的某一列添加到表单中
        grid.asSingleSelect().addValueChangeListener(event ->
                editTeacher(event.getValue()));
    }

    //搜索框和添加按钮作为一个toolbar
    private HorizontalLayout getToolbar() {
        filterText.setPlaceholder("请输入编号或姓名...");
        filterText.setClearButtonVisible(true);
        filterText.setValueChangeMode(ValueChangeMode.LAZY);
        filterText.addValueChangeListener(e -> updateList(e.getValue()));

        Button addTeacherButton = new Button("新增老师信息");
        addTeacherButton.addClickListener(click -> addTeacher());

        HorizontalLayout toolbar = new HorizontalLayout(filterText, addTeacherButton);
        toolbar.addClassName("toolbar");
        return toolbar;
    }
    
    //更新表格
    private void updateList(String value) {
        grid.setItems(teacherService.findAll(value));
    }

    //清除表单的值
    private void closeEditor() {
        teacherForm.setTeacher(null);
        teacherForm.setVisible(false);
        removeClassName("editing");
    }

    public void editTeacher(Teacher teacher) {
        if (teacher == null) {
            closeEditor();
        } else {
            teacherForm.setTeacher(teacher);
            teacherForm.setVisible(true);
            addClassName("editing");
        }
    }

    //保存
    private void saveTeacher(TeacherForm.SaveEvent event) {
        teacherService.save(event.getTeacher());
        updateList("");
        closeEditor();
    }

    //删除
    private void deleteTeacher(TeacherForm.DeleteEvent event) {
        teacherService.delete(event.getTeacher());
        updateList("");
        closeEditor();
    }

    void addTeacher() {
        grid.asSingleSelect().clear();
        editTeacher(new Teacher());
    }
}
